"use strict";
exports.__esModule = true;
exports.common = void 0;
var common = /** @class */ (function () {
  function common() {
  }
  common.prototype.getRandomCode = function (num) {
    if (num === '' || num === undefined)
      num = 6;
    var code = "";
    var array = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
      'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
      'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
      'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    ];
    for (var i = 0; i < num; i++) {
      var id = Math.round(Math.random() * 61);
      code += array[id];
    }
    return code;
  };
  common.prototype.getNumberCode = function (digit) {
    var numberCode = "";
    for (var e = 0; e < digit; e++) {
      numberCode += Math.floor(Math.random() * 10);
    }
    return numberCode;
  };
  common.prototype.navGroup = function (digit) {
      var arr = [];
      for(let i = 0; i < digit.length; i++){
        if(digit[i].level === '1'){
          digit[i].children = []
          arr.push(digit[i])
        }
      }
      let t = 0;
      do{
        if(digit[t].level === '2'){
          let ind = arr.findIndex(item => item.sort === digit[t].sort)
          arr[ind].children.push(digit[t])
        }
        t++;
      } while(t < digit.length)
      arr.sort(function(a,b){
        return a.orders - b.orders
      })
      return arr;
  };
  common.prototype.operation = function op(selectSQL) {
    return new Promise((resolve, reject) => {
      pool.getConnection(function (err, connection) {
        if (err) {
          reject(err)
        } else {
          connection.query(selectSQL, function (err, rows) {
            if (err) {
              reject(err)
            } else {
              resolve(rows)
            }
            connection.release()
          })
        }
      })
    })
  };
  common.prototype.proGroup = function (arr) {
    var newArr = [];
    for(var i = 0; i < arr.length; i++){
        for(var j = i+1; j < arr.length; j++){
            if(arr[i].name == arr[j].name){
                ++i;
            }
        }
        arr[i] = {
          id: arr[i].c_id, 
          orders: arr[i].c_orders,
          level: arr[i].level,
          sort: arr[i].sort,
          name: arr[i].name,
          product: arr.filter(item=>item.name == arr[i].name)
        }
        newArr.push(arr[i]);
    }
    return newArr;
  };

  return common;
}());

exports.common = common;
